package com.ruoyi.web.controller.applet.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.ClassType;
import com.ruoyi.common.enums.YesOrNo;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.SysAppBookchapters;
import com.ruoyi.system.domain.SysAppBookinfo;
import com.ruoyi.system.domain.SysAppClassification;
import com.ruoyi.system.dto.BookchaptersDTO;
import com.ruoyi.system.service.ISysAppBookchaptersService;
import com.ruoyi.system.service.ISysAppBookinfoService;
import com.ruoyi.system.service.ISysAppClassificationService;

/**
 * 书籍模块加载列表
 * 
 * @author PC
 *
 */
@Controller
@RequestMapping("/applet/book")
public class BookInfoController {

	@Autowired
	private ISysAppClassificationService sysAppClassificationService;

	@Autowired
	private ISysAppBookinfoService sysAppBookinfoService;

	@Autowired
	private ISysAppBookchaptersService sysAppBookchaptersService;

	@PostMapping("/homeBook")
	@ResponseBody
	public AjaxResult homeBook(String keyWord) {
		Map<String, Object> map = Maps.newConcurrentMap();
		/* 1、获取首页分类 */
		SysAppClassification appClassification = new SysAppClassification();
		appClassification.setClassType(ClassType.book.getInfo());
		List<SysAppClassification> classifications = sysAppClassificationService
				.selectSysAppClassificationList(appClassification);
		/* 2、默认加载第一个分类下的书籍 */
		SysAppBookinfo bookInfo = new SysAppBookinfo();
		bookInfo.setClassId(classifications.get(0).getId());
		bookInfo.setBookName(keyWord);
		List<SysAppBookinfo> bookinfos = sysAppBookinfoService.selectSysAppBookinfoList(bookInfo);
		map.put("classification", classifications);
		map.put("bookinfos", bookinfos);
		return AjaxResult.success("操作成功", map);
	}

	/**
	 * 根据书籍的分类id获取书籍列表
	 * 
	 * @return
	 */
	@PostMapping("/homeBookByClassId")
	@ResponseBody
	public AjaxResult homeBookByClassId(Long classId,String keyWord) {
		SysAppBookinfo bookInfo = new SysAppBookinfo();
		bookInfo.setClassId(classId);
		bookInfo.setBookStatus(YesOrNo.yes.getInfo());
		bookInfo.setBookName(keyWord);
		List<SysAppBookinfo> bookinfos = sysAppBookinfoService.selectSysAppBookinfoList(bookInfo);
		return AjaxResult.success("操作成功", bookinfos);
	}

	/**
	 * 根据数据的id获取书籍信息和章节列表信息
	 * 
	 * @Description:
	 * @Author 豫东一棵草
	 * @DateTime 2020年10月18日 下午2:07:53
	 * @param bookId
	 * @return
	 */
	@PostMapping("/getBookInfoById")
	@ResponseBody
	public AjaxResult getBookInfoById(Long bookId) {
		/* 1、获取书籍信息 */
		Map<String, Object> map = Maps.newConcurrentMap();
		SysAppBookinfo bookinfo = sysAppBookinfoService.selectSysAppBookinfoById(bookId);
		/* 2、获取书籍的章节信息 */
		SysAppBookchapters appBookchapters = new SysAppBookchapters();
		appBookchapters.setBookId(bookId);
		List<SysAppBookchapters> bookchapters = sysAppBookchaptersService.selectSysAppBookchaptersList(appBookchapters);
		List<SysAppBookchapters> chaptersFirst = bookchapters.stream().filter(s -> s.getParentId() == 0)
				.collect(Collectors.toList()); // 获取一级章节
		BookchaptersDTO chaptersDTO = null;
		List<BookchaptersDTO> chapterList = Lists.newArrayList();
		for (SysAppBookchapters chapter : chaptersFirst) {
			chaptersDTO = new BookchaptersDTO();
			BeanUtils.copyBeanProp(chaptersDTO, chapter);
			/* 获取一级分类小的二级数据分类 */
			List<SysAppBookchapters> chapters = bookchapters.stream()
					.filter(s -> s.getParentId().equals(chapter.getDeptId())).collect(Collectors.toList()); // 获取一级章节
			chaptersDTO.setBookchapters(chapters);
			chapterList.add(chaptersDTO);
		}
		map.put("bookInfo", bookinfo);
		map.put("chapterList", chapterList);
		return AjaxResult.success("操作成功", map);
	}

	/**
	 * 根据id获取章节信息
	 * 
	 * @Description:获取书籍章节详情
	 * @Author 豫东一棵草
	 * @DateTime 2020年10月18日 下午2:15:03
	 * @param id
	 * @return
	 */
	@PostMapping("/getBookChapterById")
	@ResponseBody
	public AjaxResult getBookChapterById(Long id) {
		/* 1、获取章节信息 */
		Map<String, Object> map = Maps.newConcurrentMap();
		SysAppBookchapters bookchapter = sysAppBookchaptersService.selectSysAppBookchaptersById(id);
		map.put("bookchapter", bookchapter);
		return AjaxResult.success("操作成功", map);
	}

}
